package com.dbb.trade.entity;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;

import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import com.dbb.trade.util.FileUtils;
import com.dbb.trade.util.ImageUtil;
import com.drew.imaging.jpeg.JpegMetadataReader;
import com.drew.imaging.jpeg.JpegProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.MetadataException;
import com.drew.metadata.exif.ExifIFD0Directory;
import com.drew.metadata.exif.ExifSubIFDDirectory;

@Entity
@DiscriminatorValue("photo")
public class Photo extends FileResource {
	
	public static final Integer LOGO_SIZE = 200;
	
	public static final String FILEPATH = "old/";
	public static final String LOGOPATH = "logo/";
	public static final String INDEXPATH = "index/";
	
	private static Logger logger = LoggerFactory.getLogger(Photo.class);
	/*
	 * 照片拍摄日期
	 */
    private Date dcCreateDate;
    /*
	 * 曝光时间
	 */
    private String exposureTime;
    /*
   	 * 光圈值
   	 */
    private String fNumber;
    /*
   	 * ISO 速度
   	 */
    private String iSOSpeedRatings;
    /*
   	 * 曝光时间
   	 */
    private String shutterSpeedValue; 
    /*
   	 * 曝光补偿
   	 */
    private String exposureBiasValue;
    /*
   	 * 测光模式
   	 */
    private String meteringMode; 
    /*
   	 * 焦距
   	 */
    private String focalLength; 
    /*
   	 * 照相机型号
   	 */
    private String model; 
    /*
   	 * 照相生产商
   	 */
    private String make; 
	private int width; 
	private int height;
    
    public Photo() {
    }
    public Photo(File file) {
    	super(file);
    	InputStream is = null;
		try {
			is = new FileInputStream(file);
		} catch (FileNotFoundException ex) {
			logger.error("file init fail!:" + ex.getMessage());
		}
		if(is != null && "image/jpeg".equals(super.getContentType())) {
	    	Metadata metadata;
			try {
				metadata = JpegMetadataReader.readMetadata(is);
		        ExifSubIFDDirectory esif = metadata.getDirectory(ExifSubIFDDirectory.class);
		        ExifIFD0Directory eif = metadata.getDirectory(ExifIFD0Directory.class);
		        this.dcCreateDate = eif.getDate(ExifIFD0Directory.TAG_DATETIME);
		        this.make = eif.getDescription(ExifIFD0Directory.TAG_MAKE);
		        this.model = eif.getDescription(ExifIFD0Directory.TAG_MODEL);
		        this.exposureTime = esif.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_TIME);
		        this.fNumber = esif.getDescription(ExifSubIFDDirectory.TAG_FNUMBER);
		        this.iSOSpeedRatings = esif.getDescription(ExifSubIFDDirectory.TAG_ISO_EQUIVALENT);
		        this.shutterSpeedValue = esif.getDescription(ExifSubIFDDirectory.TAG_SHUTTER_SPEED);
		        this.exposureBiasValue = esif.getDescription(ExifSubIFDDirectory.TAG_EXPOSURE_BIAS);
		        this.meteringMode = esif.getDescription(ExifSubIFDDirectory.TAG_METERING_MODE);
		        this.focalLength = esif.getDescription(ExifSubIFDDirectory.TAG_FOCAL_LENGTH);
		        this.width = esif.getInt(ExifSubIFDDirectory.TAG_EXIF_IMAGE_WIDTH);
		        this.height = esif.getInt(ExifSubIFDDirectory.TAG_EXIF_IMAGE_HEIGHT);
			} catch (JpegProcessingException e) {
				logger.error(file.getName() + "::" + e.getMessage());
			} catch (MetadataException e) {
				logger.error(file.getName() + "::" + e.getMessage());
			}
		}
    }
    
    public String getExposureTime() {
        return exposureTime;
    }

    public void setExposureTime(String exposureTime) {
        this.exposureTime = exposureTime;
    }

    public String getfNumber() {
        return fNumber;
    }

    public void setfNumber(String fNumber) {
        this.fNumber = fNumber;
    }

    public String getiSOSpeedRatings() {
        return iSOSpeedRatings;
    }

    public void setiSOSpeedRatings(String iSOSpeedRatings) {
        this.iSOSpeedRatings = iSOSpeedRatings;
    }

    public String getExposureBiasValue() {
        return exposureBiasValue;
    }

    public void setExposureBiasValue(String exposureBiasValue) {
        this.exposureBiasValue = exposureBiasValue;
    }

    public String getMeteringMode() {
        return meteringMode;
    }

    public void setMeteringMode(String meteringMode) {
        this.meteringMode = meteringMode;
    }

    public String getFocalLength() {
        return focalLength;
    }

    public void setFocalLength(String focalLength) {
        this.focalLength = focalLength;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

	public String getShutterSpeedValue() {
		return shutterSpeedValue;
	}

	public void setShutterSpeedValue(String shutterSpeedValue) {
		this.shutterSpeedValue = shutterSpeedValue;
	}

	public String getMake() {
		return make;
	}

	public void setMake(String make) {
		this.make = make;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public void initFile(MultipartFile multipartFile, String string) throws IOException {
		File f1 = new File(string + FILEPATH);
		if(!f1.exists()) {f1.mkdirs();}
		FileUtils.copy(multipartFile.getInputStream(), string + FILEPATH + super.getId());
		f1 = new File(string + LOGOPATH);
		if(!f1.exists()) {f1.mkdirs();}
		ImageUtil.resize(string + FILEPATH + super.getId(), string + LOGOPATH + super.getId(), LOGO_SIZE);
		f1 = new File(string + INDEXPATH);
	}
	
	public void initFile(File file, String string) throws IOException {
		File f1 = new File(string + FILEPATH);
		if(!f1.exists()) {f1.mkdirs();}
		FileUtils.copy(new FileInputStream(file), string + FILEPATH + super.getId());
		f1 = new File(string + LOGOPATH);
		if(!f1.exists()) {f1.mkdirs();}
		ImageUtil.resize(string + FILEPATH + super.getId(), string + LOGOPATH + super.getId(), LOGO_SIZE);
		f1 = new File(string + INDEXPATH);
	}
	public Date getDcCreateDate() {
		return dcCreateDate;
	}
	public void setDcCreateDate(Date dcCreateDate) {
		this.dcCreateDate = dcCreateDate;
	}

}
